<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            padding: 20px;
        }

        .container {
            width: 100%;
            max-width: 500px;
            background-color: #fff;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(to right, #6a11cb, #2575fc);
            color: white;
            text-align: center;
            padding: 25px 20px;
        }

        .header h1 {
            font-size: 28px;
            margin-bottom: 10px;
        }

        .header p {
            font-size: 16px;
            opacity: 0.9;
        }

        .form-container {
            padding: 30px;
        }

        .form-group {
            margin-bottom: 20px;
            position: relative;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
        }

        .form-group input {
            width: 100%;
            padding: 14px;
            border: 2px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s;
        }

        .form-group input:focus {
            border-color: #6a11cb;
            outline: none;
        }

        .form-group i {
            position: absolute;
            right: 15px;
            top: 42px;
            color: #999;
            font-size: 18px;
        }

        .btn-register {
            width: 100%;
            padding: 15px;
            background: linear-gradient(to right, #6a11cb, #2575fc);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 18px;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.3s, box-shadow 0.3s;
        }

        .btn-register:hover {
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(106, 17, 203, 0.4);
        }

        .btn-register:active {
            transform: translateY(0);
        }

        .message {
            padding: 15px;
            margin-top: 20px;
            border-radius: 8px;
            text-align: center;
            font-weight: 500;
            display: none;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .login-link {
            text-align: center;
            margin-top: 25px;
            color: #666;
        }

        .login-link a {
            color: #6a11cb;
            text-decoration: none;
            font-weight: 600;
        }

        .login-link a:hover {
            text-decoration: underline;
        }

        .password-toggle {
            cursor: pointer;
        }

        @media (max-width: 576px) {
            .container {
                border-radius: 10px;
            }

            .form-container {
                padding: 20px;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>创建账户</h1>
        <p>填写以下信息完成注册</p>
    </div>

    <div class="form-container">
        <form id="regForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" placeholder="请输入用户名" required>
                <i class="fas fa-user"></i>
            </div>

            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入密码" required>
                <i class="password-toggle fas fa-eye"></i>
            </div>

            <div class="form-group">
                <label for="email">电子邮箱</label>
                <input type="email" id="email" name="email" placeholder="请输入电子邮箱" required>
                <i class="fas fa-envelope"></i>
            </div>

            <div class="form-group">
                <label for="phone">手机号码</label>
                <input type="tel" id="phone" name="phone" placeholder="请输入手机号码" required>
                <i class="fas fa-phone"></i>
            </div>

            <button type="submit" class="btn-register">注册账户</button>
        </form>

        <div id="message" class="message"></div>

        <div class="login-link">
            已有账户？<a href="#">立即登录</a>
        </div>
    </div>
</div>

<!-- Font Awesome 图标 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/js/all.min.js"></script>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const form = document.getElementById('regForm');
        const messageDiv = document.getElementById('message');
        const passwordToggle = document.querySelector('.password-toggle');

        // 密码显示/隐藏功能
        passwordToggle.addEventListener('click', function() {
            const passwordInput = document.getElementById('password');
            if (passwordInput.type === 'password') {
                passwordInput.type = 'text';
                this.classList.remove('fa-eye');
                this.classList.add('fa-eye-slash');
            } else {
                passwordInput.type = 'password';
                this.classList.remove('fa-eye-slash');
                this.classList.add('fa-eye');
            }
        });

        // 表单提交事件
        form.addEventListener('submit', async function(e) {
            e.preventDefault();

            // 获取表单数据
            const formData = {
                username: document.getElementById('username').value,
                password: document.getElementById('password').value,
                email: document.getElementById('email').value,
                phone: document.getElementById('phone').value
            };

            // 简单前端验证
            if (!formData.username || !formData.password || !formData.email || !formData.phone) {
                showMessage('请填写所有必填字段', 'error');
                return;
            }

            // 发送注册请求
            try {
                const response = await fetch('/v1/user/reg', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(formData)
                });

                const result = await response.json();

                if (result.state === 2000) {
                    // 注册成功
                    showMessage('注册成功！即将跳转到登录页面...', 'success');
                    form.reset();

                    // 模拟跳转（实际应用中会真的跳转）
                    setTimeout(() => {
                        showMessage('跳转到登录页面...', 'success');
                    }, 2000);
                } else if (result.state === 1000) {
                    // 用户名被占用
                    showMessage('用户名已被占用，请尝试其他用户名', 'error');
                } else {
                    // 其他错误
                    showMessage('注册失败：' + result.message, 'error');
                }
            } catch (error) {
                console.error('注册请求错误:', error);
                showMessage('网络错误，请稍后重试', 'error');
            }
        });

        // 显示消息函数
        function showMessage(text, type) {
            messageDiv.textContent = text;
            messageDiv.className = 'message ' + type;
            messageDiv.style.display = 'block';

            // 5秒后自动隐藏消息
            setTimeout(() => {
                messageDiv.style.display = 'none';
            }, 5000);
        }
    });
</script>
</body>
</html>